/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.propagation.sampling;

import ore.SpacecraftState;
import ore.errors.OrekitException;
import ore.errors.PropagationException;
import ore.propagation.BasicPropagator;
import ore.time.AbsoluteDate;

/** 
 * Implementation of the {@link OrekitStepInterpolator} interface
 * based on a {@link BasicPropagator}.
 * 
 * @author Luc Maisonobe
 */
public class BasicStepInterpolator
    extends Object
    implements OrekitStepInterpolator
{

    /** 
     * Underlying propagator.
     */
    private final BasicPropagator propagator;
    /** 
     * Previous date.
     */
    private AbsoluteDate previousDate;
    /** 
     * Current date.
     */
    private AbsoluteDate currentDate;
    /** 
     * Interpolated State.
     */
    private SpacecraftState interpolatedState;
    /** 
     * Forward propagation indicator.
     */
    private boolean forward;


    /** 
     * @param propagator Underlying propagator to use
     */
    public BasicStepInterpolator(BasicPropagator propagator) {
        super();
        this.propagator  = propagator;
        this.previousDate     = AbsoluteDate.PAST_INFINITY;
        this.currentDate      = AbsoluteDate.PAST_INFINITY;
    }


    /**
     * {@inheritDoc}
     */
    public AbsoluteDate getCurrentDate() {
        return this.currentDate;
    }
    /**
     * {@inheritDoc}
     */
    public AbsoluteDate getInterpolatedDate() {
        return this.interpolatedState.getDate();
    }
    /**
     * {@inheritDoc}
     */
    public SpacecraftState getInterpolatedState() throws OrekitException {
        return this.interpolatedState;
    }
    /**
     * {@inheritDoc}
     */
    public AbsoluteDate getPreviousDate() {
        return this.previousDate;
    }
    /**
     * {@inheritDoc}
     */
    public boolean isForward() {
        return this.forward;
    }
    /**
     * {@inheritDoc}
     */
    public void setInterpolatedDate(AbsoluteDate date)
        throws PropagationException
    {
        this.interpolatedState = this.propagator.propagate(date);
    }
    /** 
     * Shift one step forward.  Copy the current date into the
     * previous date, hence preparing the interpolator for future
     * calls to {@link #storeDate storeDate}.
     */
    public void shift() {
        this.previousDate = this.currentDate;
    }
    /** 
     * Store the current step date.
     * @param date Current step date
     * @exception PropagationException The state cannot be propagated
     * at the specified date
     */
    public void storeDate(AbsoluteDate date)
        throws PropagationException
    {
        this.currentDate = date;
        this.forward     = (currentDate.compareTo(previousDate) >= 0);
        this.setInterpolatedDate(currentDate);
    }
}
